using System;
using Jalal.Library.DataStructure;
using System.Data;
using Jalal.Library.Data;
using BCGC.DAL;
using Jalal.Library.Utility;
namespace BCGC.BLL.Forum
{

    public class PostCategory
    {

        private int categoryID;
        public int CategoryID
        {
            get
            {
                return categoryID;
            }
        }

        private String categoryName;
        public String CategoryName
        {
            get
            {
                return categoryName;
            }
            set
            {
                categoryName = value;
            }
        }

        private String description;

        public String Description
        {
            get { return description; }
            set { description = value; }
        }

        private bool isActive;
        public bool IsActive
        {
            get
            {
                return isActive;
            }
            set
            {
                isActive = value;
            }
        }

        private int totalPost;
        public int TotalPost
        {
            get
            {
                return totalPost;
            }
            set
            {
                totalPost = value;
            }
        }

        internal PostCategory() { }

        internal PostCategory(DataRow dataRow)
        {
            if (dataRow != null)
            {
                categoryID = (int)CommonUtility.FilterNull(dataRow["CategoryID"], typeof(int));
                categoryName = (string)CommonUtility.FilterNull(dataRow["CategoryName"], typeof(string));
                description = (string)CommonUtility.FilterNull(dataRow["Description"], typeof(string));
                isActive = (bool)CommonUtility.FilterNull(dataRow["IsActive"], typeof(bool));
                totalPost = (int)CommonUtility.FilterNull(dataRow["TotalPost"], typeof(int));
                
            }
         }

        private static ReadWriteCustomCollection<PostCategory> GetCollection(DataRowCollection rowCollection)
        {
            ReadWriteCustomCollection<PostCategory> collection = new ReadWriteCustomCollection<PostCategory>();

            foreach (DataRow dr in rowCollection)
            {
                collection.Add(new PostCategory(dr));
            }
            return collection;
        }

        internal int Create()
        {
            DataFactory.GetDataMiner().BeginTransaction(true);
            int result = PostCategoryData.CreatePostCategory(this.CategoryName,this.description,this.TotalPost,this.IsActive);
            DataFactory.GetDataMiner().CommitTransaction();
            return result;
        }

        internal bool Update()
        {
            DataFactory.GetDataMiner().BeginTransaction(true);
            bool result = PostCategoryData.UpdatePostCategory(this.CategoryID, this.CategoryName,this.description, this.TotalPost, this.IsActive);
            DataFactory.GetDataMiner().CommitTransaction();
            return result;
        }

        internal bool Save()
        {
            if (this.CategoryID != 0)
            {
                return Update();
            }
            else
            {
                int result = Create();
                if (result > 0)
                {
                    this.categoryID = result;
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        public static bool Delete(int id)
        {
            throw new System.Exception("Not implemented");
        }

        public static ReadWriteCustomCollection<PostCategory> GetAll()
        {
            DataFactory.GetDataMiner().BeginTransaction(true);
            DataTable result = PostCategoryData.GetAllPostCategory();
            DataFactory.GetDataMiner().CommitTransaction();

            if (result.Rows.Count > 0)
                return GetCollection(result.Rows);
            else
                return null;
        }


        public static PostCategory GetPostCategoryByID(int id)
        {
            DataFactory.GetDataMiner().BeginTransaction(true);
            DataTable result = PostCategoryData.GetPostCategoryByID(id);
            DataFactory.GetDataMiner().CommitTransaction();

            if (result.Rows.Count > 0)
                return new PostCategory(result.Rows[0]);
            else
                return null;
        }


    }

}
